﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;

public partial class Bidder_CompetePrice1 : System.Web.UI.Page
{
    int prjid;
    public string status;
    String CP1strConditions;
    Int64 CompetepriceAccountid_TBR;
    private const int GVgoodsid = 0;
    private const int GVpoductname = 1;
    private const int GVMedimite = 2;
    private const int GVoutlookc = 3;
    private const int GVcompanyname = 4;
    private const int GVfactor = 5;
    private const int GVspprice = 6;
    // private const int GVlimitprice=7;
    private const int GVCompetePackPrice1 = 7;
    private const int GVCompetePackPrice2 = 8;
    private const int GVCompetePackPrice3 = 9;
    private const int GVCompetitionSwich = 10;
    private const int GVCompeteNum = 11;
    private const int GVCompetePackPrice1V = 12;
    private const int GVCompetePackPrice2V = 13;
    private const int GVbidid = 14;
    private const int CompeteMinPrice1 = 15;      //最小制剂报价

    private const int CompeteMinPrice2 = 16;
    private const int GVbidCount = 17;               //竞价投标人数
    private const int GVAlreadyIsbidNum = 18;  //该竞价组已中标数
    private const int GVshouldisbidnum = 19;     //应该中标数

    private const int GVlessnum = 20;       //该竞价组还剩中标数 
    private const int GVShow = 21;       //显示
    private const int GVlassCount = 22;        //该竞价组本次竞价投标人数(低于标底的)

    string strNumber = "1";   //竞价次数
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            ErrorMsg.Text = "";
            prjid = Int32.Parse(SysFun.IsSQL(Request.QueryString["projectid"]));

            IsCompeteTablev();//是否开启竞价

            CompetitionNum();//查询是第几次竞价
            if (CheckPerson())
            {
                if (Request.Form["SaveCPrice"] == "提交本页报价")
                {
                    string s = "";
                    ErrorMsg.Text = "";
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        if (row.RowType == DataControlRowType.DataRow)
                        {


                            string a = Request.Form[row.Cells[GVbidid].Text];
                            if (!SysFun.IsNumeric(a))//判断输入的是否是数值
                            {
                                ErrorMsg.Text += row.Cells[GVgoodsid].Text + "报价只能为数字<BR />";
                                Response.Write("<script>window.alert('报价只能为数字！');</script>");
                            }
                            else
                            {
                                a = Convert.ToDecimal(a).ToString("N2");
                                //判断当前输入得值是否越界,
                                if (SysFun.ToDec(a) < 0 || SysFun.ToDec(a) > 99999999)
                                {
                                    ErrorMsg.Text += row.Cells[GVgoodsid].Text + "报价数据有误<BR />";
                                    Response.Write("<script>window.alert('报价数据有误！');</script>");
                                }
                                else
                                {
                                    if (strNumber == "1")//第一次报价
                                    {
                                        //判断输入得价格是否高于抱价

                                        if (SysFun.ToDec(a) >= SysFun.ToDec(row.Cells[GVspprice].Text))
                                        {
                                            ErrorMsg.Text += row.Cells[GVgoodsid].Text + "报价不能高于第一次价<BR />";
                                            Response.Write("<script>window.alert('报价不能高于第一次价！');</script>");
                                        }
                                        else
                                        {
                                            //判断当前价格是否被修改过，如果没有修改，就不将此价格放到提交列表中

                                            if (a != row.Cells[GVCompetePackPrice1V].Text.ToString())
                                            {

                                                string _strLimtPrice = Convert.ToDecimal(Convert.ToDecimal(a) / (Convert.ToDecimal(row.Cells[GVfactor].Text))).ToString("N4");
                                                _strLimtPrice = _strLimtPrice.Replace(",", "");
                                                a = a.Replace(",", "");
                                                s += "@" + "update CompeteTable set CompetePackPrice1=" + a + ",CompeteMinPrice1=" + _strLimtPrice + " where bidid=" + row.Cells[GVbidid].Text;
                                            }
                                        }
                                    }
                                    else  //第二次报价
                                    {
                                        //判断输入得价格是否高于参考价
                                        if (SysFun.ToDec(a) > SysFun.ToDec(row.Cells[GVCompetePackPrice1V].Text))
                                        {
                                            ErrorMsg.Text += row.Cells[GVgoodsid].Text + "第二次报价不能高于第一次<BR />";
                                            Response.Write("<script>window.alert('第二次报价不能高于第一次！');</script>");
                                        }
                                        else
                                        {
                                            //判断当前价格是否被修改过，如果没有修改，就不将此价格放到提交列表中

                                            if (a != row.Cells[GVCompetePackPrice1V].Text.ToString())
                                            {

                                                string _strLimtPrice = Convert.ToDecimal(Convert.ToDecimal(a) / (Convert.ToDecimal(row.Cells[GVfactor].Text))).ToString("N4");
                                                _strLimtPrice = _strLimtPrice.Replace(",", "");
                                                a = a.Replace(",", "");
                                                s += "@" + "update CompeteTable set CompetePackPrice2=" + a + ",CompeteMinPrice2=" + _strLimtPrice + " where bidid=" + row.Cells[GVbidid].Text;
                                            }
                                        }
                                    }

                                }
                            }

                        }
                    }
                    Db db = new Db();
                    if (s.Length > 0)
                    {
                        db.ExecuteTransaction(s.Substring(1).Split(new char[] { '@' }), 30);
                        ErrorMsg.Text += "本页报价成功，未报价做零处理";
                    }
                    db.Close();

                }
                showGridView();
            }
            else
            {
                Response.Write("<script>window.alert('对不起,本次竞价您没有竞价品种！');</script>");
                Response.Write("<script>window.location=\"Nothing.aspx?Projectid=" + prjid.ToString() + "\"</script>");
            }
        }

    }

    public void showGridView()
    {
        CP1strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = @"V_CompetePrice left join tempProductIdGroup1 on tempProductIdGroup1.bidtableid=V_CompetePrice.bidtableid ";                                               //要查询的表明
        dbPage.Key = "Bidid";                                               //表的主键    
        dbPage.Where = CP1strConditions;                                           //查询条件    
        dbPage.SelectField = "*,shouldisbidnum-AlreadyIsbidNum  as lessnum,tempProductIdGroup1.BidNum as bidnumber";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "V_CompetePrice.ProductName,V_CompetePrice.Medicinemodel,V_CompetePrice.Outlookc,Bidid";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    public string fctConditions()
    {
        string strtemp;
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {

            CompetepriceAccountid_TBR = SysFun.ToLong(_Account.AccountId);
            strtemp = " AccountID='" + CompetepriceAccountid_TBR + "' and CheckStatus='1' And ProjectId=" + prjid.ToString() + " And Status=1 and isbid <>1 and CompetitionNum=" + strNumber + " And";
            //药品通用名

            string CP1PN = SysFun.ToTrim(ProductName.Text);
            if (CP1PN != "")
            {
                strtemp += " ProductName like '%" + CP1PN + "%' And";
            }
            //药品流水号

            string CP1Goodsid = txtGoodsId.Text.Trim();
            if (CP1Goodsid != "")
            {
                strtemp += " Goodsid ='" + CP1Goodsid + "' And";
            }

            //生产企业名称
            string CP1ComSC = CompanyName.Text.Trim();
            if (CP1ComSC != "")
            {
                strtemp += " CompanyName like '%" + CP1ComSC + "%' And";
            }
            //竞价报价
            string _strPriceState = lstPriceState.SelectedValue;
            switch (_strPriceState)
            {
                case "全部":
                    break;
                case "未报价":
                    if (strNumber == "1")
                    {
                        strtemp += " (CompetePackPrice1=0 or CompetePackPrice1 is null) and";
                    }
                    if (strNumber == "2")
                    {
                        strtemp += " (CompetePackPrice2=0 or CompetePackPrice2 is null) and";
                    }
                    break;
                case "已报价":
                    if (strNumber == "1")
                    {
                        strtemp += " CompetePackPrice1>0 and";
                    }
                    if (strNumber == "2")
                    {
                        strtemp += " CompetePackPrice2>0 and";
                    }
                    break;
            }

            if (strtemp != "")
            {
                strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
            }
        }
        else
        {
            strtemp = "";

        }
        return strtemp;


    }
    /// <summary>
    /// 处理输入的价格

    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[GVCompetePackPrice1].Visible = false;
        e.Row.Cells[GVCompetePackPrice2].Visible = false;
        e.Row.Cells[GVCompetePackPrice3].Visible = false;
        e.Row.Cells[CompeteMinPrice1].Visible = false;
        e.Row.Cells[CompeteMinPrice2].Visible = false;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[GVCompetitionSwich].Text == "0")//竞价开关是否开
            {
                e.Row.Cells[GVCompetePackPrice1V].Visible = true;
                status = "none";
            }
            else if (e.Row.Cells[GVCompeteNum].Text.Trim() == "1")//第一次竞价
            {
                e.Row.Cells[GVCompetePackPrice1].Visible = true;
                GridView1.HeaderRow.Cells[GVCompetePackPrice1].Visible = true;
                e.Row.Cells[GVCompetePackPrice1].Text = "<input type=\"text\" style=\"width:80px\" name='" + e.Row.Cells[GVbidid].Text + "' value=\"" + SysFun.ToDec(e.Row.Cells[GVCompetePackPrice1V].Text) + "\" /> ";
                status = "block";
            }
            else if (e.Row.Cells[GVCompeteNum].Text.Trim() == "2")//第二次竞价
            {
                e.Row.Cells[GVCompetePackPrice1].Visible = true;
                e.Row.Cells[GVCompetePackPrice2].Visible = true;
                e.Row.Cells[CompeteMinPrice2].Visible = true;
                GridView1.HeaderRow.Cells[GVCompetePackPrice1].Visible = true;
                GridView1.HeaderRow.Cells[GVCompetePackPrice2].Visible = true;
                e.Row.Cells[GVCompetePackPrice1].Text = e.Row.Cells[GVCompetePackPrice1V].Text;
                e.Row.Cells[GVCompetePackPrice2].Text = "<input type=\"text\" style=\"width:80px\" name='" + e.Row.Cells[GVbidid].Text + "' value=\"" + SysFun.ToDec(e.Row.Cells[GVCompetePackPrice2V].Text) + "\" /> ";
            }

            else
            {
                e.Row.Cells[GVCompetePackPrice1].Visible = true;
                e.Row.Cells[GVCompetePackPrice2].Visible = true;
                e.Row.Cells[GVCompetePackPrice3].Visible = true;
                GridView1.HeaderRow.Cells[GVCompetePackPrice1].Visible = true;
                GridView1.HeaderRow.Cells[GVCompetePackPrice2].Visible = true;
                GridView1.HeaderRow.Cells[GVCompetePackPrice3].Visible = true;
                e.Row.Cells[GVCompetePackPrice1].Text = e.Row.Cells[GVCompetePackPrice1V].Text;
                e.Row.Cells[GVCompetePackPrice2].Text = e.Row.Cells[GVCompetePackPrice2V].Text;
                status = "none";
            }
            e.Row.Cells[GVShow].Text = "<input type =button  name='查看' onclick=\"window.open('D_detail.aspx>',height=200,width=400,status=yes,toolbar=no,menubar=no,location=no\"); style='width: 38px; height: 22px' value='查看'>";
            e.Row.Cells[GVlassCount].Visible = true;
            GridView1.HeaderRow.Cells[GVlassCount].Visible = true;
            e.Row.Cells[GVShow].Text = "<a href=\"javascript:openwin('" + e.Row.Cells[GVbidCount].Text + "','" + e.Row.Cells[GVAlreadyIsbidNum].Text + "','" + e.Row.Cells[GVshouldisbidnum].Text + "','" + e.Row.Cells[GVlassCount].Text + "'," + strNumber + ")\">查看</a>";
            e.Row.Cells[GVlassCount].Visible = false;
            GridView1.HeaderRow.Cells[GVlassCount].Visible = false;
        }
        if (strNumber == "1")
        {
            e.Row.Cells[CompeteMinPrice1].Visible = true;

        }
        if (strNumber == "2")
        {
            e.Row.Cells[CompeteMinPrice2].Visible = true;
        }
        e.Row.Cells[GVbidid].Visible = false;
        e.Row.Cells[GVCompetitionSwich].Visible = false;
        e.Row.Cells[GVCompeteNum].Visible = false;
        e.Row.Cells[GVCompetePackPrice1V].Visible = false;
        e.Row.Cells[GVCompetePackPrice2V].Visible = false;

    }
    /// <summary>
    /// 检查登录人的身份

    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public Boolean CheckPerson()
    {    //马毅 0928 不需解密
        //String JJPwdPrice = SysFun.PasswordDecrypt(Request.QueryString["JJPwd"]);//竞价报价密码
        //String JJRondPrice = SysFun.PasswordDecrypt(Request.QueryString["JJRond"]);//竞价随机密码
        //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];//当前用户得AccountId
        //if (_Account != null)
        //{
        //    bool _boolRowsCount = false;
        //    CompetepriceAccountid_TBR = SysFun.ToLong(_Account.AccountId);
        //    Db db = new Db();
        //    String _WhereSql = " Status=1 And ProjectId=" + prjid.ToString() + " And AccountId=" + CompetepriceAccountid_TBR.ToString() + " And CompetePassword='" + JJPwdPrice + "' And CompeteRetinuePassword='" + JJRondPrice + "' And CompetitionSwich=1";
        //    String Sql = "Select TOP 1 * From CompeteCheck Where " + _WhereSql;
        //    if (db.DataTable(Sql).Rows.Count > 0)
        //    {
        //        _boolRowsCount = true;
        //    }
        //    else
        //    {
        //        _boolRowsCount = false;
        //    }
        //    db.Close();
        //    return _boolRowsCount;
        //}
        //else 
        //{
        //    return false;
        //}

        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];//当前用户得AccountId
        if (_Account != null)
        {
            bool _boolRowsCount = false;
            CompetepriceAccountid_TBR = SysFun.ToLong(_Account.AccountId);
            string strseelt = "select CompeteTablevs.* from CompeteTablevs where CompeteNum=" + strNumber;
            Db db = new Db();
            String _WhereSql = " Status=1 And ProjectId=" + prjid.ToString() + " And AccountId=" + CompetepriceAccountid_TBR.ToString() + " And CompetitionSwich=1";
            String Sql = "Select TOP 1 * From CompeteTablevs Where " + _WhereSql;
            if (db.DataTable(Sql).Rows.Count > 0)
            {
                _boolRowsCount = true;
            }
            else
            {
                _boolRowsCount = false;
            }
            db.Close();
            return _boolRowsCount;
        }
        else
        {
            return false;
        }
    }

    //是否开启竞价

    private void IsCompeteTablev()
    {
        string strsele = "select max(CompetitionSwich) as CompetitionSwich  from project_bid where projectid=" + prjid;
        Db dbdd = new Db();
        DataTable dt = dbdd.DataTable(strsele);
        dbdd.Close();
        if (SysFun.ToInt(dt.Rows[0].ItemArray[0].ToString()) == 0)
        {
            Response.Write("<script>alert('竞价开关未开启！');</script>");
            Response.Write("<script>window.location=\"Nothing.aspx?Projectid=" + prjid.ToString() + "\"</script>");
        }
    }

    //查询是第几次竞价
    private void CompetitionNum()
    {
        string strsele = "select isnull(max(CompetitionNum),1) as CompetitionNum  from project_bid where projectid=" + prjid;
        Db dbdd = new Db();
        DataTable dt = dbdd.DataTable(strsele);
        dbdd.Close();
        int numbre = Convert.ToInt32(dt.Rows[0].ItemArray[0].ToString());
        if ((numbre == 1) || (numbre == 0))
        { strNumber = "1"; }
        else if (numbre == 2)
        { strNumber = "2";
        ErrorMsg.Text = "尊敬的投标人，贵公司所投标的下列品规高于底价未能入围，需进行第三次报价，第三次报价时，你的底价自动下浮3%，请把握好机会，在规定时间内进行报价。";
        }
        else
        { strNumber = "3"; }
        dt.Dispose();
        if (numbre >= 2)
        {
            Response.Write("<script>alert('竞价已经结束！');</script>");
            Response.Write("<script>window.location=\"Nothing.aspx?Projectid=" + prjid.ToString() + "\"</script>");
        }
    }
}
